var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://player:player@127.0.0.1:27888/mdb'; // 数据库为 runoob
var bearcat = require('bearcat');
var async = require('async');


var MongoDb = function (opts) {

}

MongoDb.prototype.init = function () {
}
//增加
MongoDb.prototype.insertData = function (tableName, data, callback) {
    async.waterfall([
        function (cb) {
            MongoClient.connect(DB_CONN_STR, function (err, db) {
                cb(err, db);
            });
        },
        function (db, cb) {
            var collection = db.collection(tableName);
            collection.insert(data, function (err, result) {
                if (err) {

                }
                cb(err, result, db);
            });
        }
    ],
        function (err, result, db) {
            if (err) {

            }
            else {

            }
            console.info("result = " + result);
            db.close();
            callback(err, result);
        });
}
//查找
MongoDb.prototype.selectData = function (tableName, whereStr, callback) {
    async.waterfall([
        function (cb) {
            MongoClient.connect(DB_CONN_STR, function (err, db) {
                cb(err, db);
            });
        },
        function (db, cb) {
            var collection = db.collection(tableName);
            collection.find(whereStr).toArray(function (err, result) {
                if (err) {

                }
                cb(err, result, db);
            });
        }
    ],
        function (err, result, db) {
            if (err) {

            }
            else {

            }
            db.close();
            console.log("result = " + JSON.stringify(result));
            callback(err, result);
        });
}
//改
MongoDb.prototype.updateData = function (tableName, whereStr, updateStr, callback) {
    async.waterfall([
        function (cb) {
            MongoClient.connect(DB_CONN_STR, function (err, db) {
                cb(err, db);
            });
        },
        function (db, cb) {
            var collection = db.collection(tableName);
            collection.update(whereStr, updateStr, function (err, result) {
                if (err) {

                }
                cb(err, result, db);
            });
        }
    ],
        function (err, result, db) {
            if (err) {

            }
            else {

            }
            db.close();
            console.log("result = " + JSON.stringify(result));
            callback(err, result);
        });
}


MongoDb.prototype.removeData = function (tableName, whereStr, callback) {
    async.waterfall([
        function (cb) {
            MongoClient.connect(DB_CONN_STR, function (err, db) {
                cb(err, db);
            });
        },
        function (db, cb) {
            var collection = db.collection(tableName);
            collection.remove(whereStr, function (err, result) {
                if (err) {

                }
                cb(err, result, db);
            });
        }
    ],
        function (err, result, db) {
            if (err) {

            }
            else {

            }
            db.close();
            console.log("result = " + JSON.stringify(result));
            callback(err, result);
        });
}


MongoDb.prototype.getPlayerId = function (callback) {
    MongoClient.connect(DB_CONN_STR, function (err, db) {
        var collection = db.collection("uids");
        var sequenceDocument = collection.findAndModify({ _id: 'ids' },
            [['_id', 'asc']],
            { $inc: { seq: 1 } },
            { new: true, upsert: true },
            function (err, result) {
                db.close();
                callback(result.value.seq);
            })
    });
}
module.exports = {
    id: "mongoDb",
    func: MongoDb,
    init: "init",
    args: [{
        name: "opts",
        type: "Object"
    }],
    props: [{
        name: "consts",
        ref: "consts"
    }]
};




